package l1j.server.server.model.item;

import static l1j.server.server.model.skill.L1SkillId.ABSOLUTE_BARRIER;
import l1j.server.server.ClientThread;
import l1j.server.server.model.L1Location;
import l1j.server.server.model.L1Teleport;
import l1j.server.server.model.L1World;
import l1j.server.server.model.Instance.L1ItemInstance;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.serverpackets.S_ServerMessage;
import l1j.server.server.serverpackets.S_SystemMessage;
import l1j.server.server.templates.L1BookMark;

public class L1Scroll {
	public L1Scroll(){
	}
	public void use(L1PcInstance pc,ClientThread client,L1ItemInstance l1iteminstance1,L1ItemInstance l1iteminstance,int itemId,int btele) throws Exception {
		if (itemId == 140100 || itemId == 40100 || itemId == 40099 || itemId == 40086 || itemId == 40863) {
			L1BookMark bookm = pc.getBookMark(btele);
			if (bookm != null) {
				if (pc.getMap().isEscapable() || pc.isGm()) {
					int newX = bookm.getLocX();
					int newY = bookm.getLocY();
					short mapId = bookm.getMapId();

					if (itemId == 40086) { // マステレポートスクロール
						for (L1PcInstance member : L1World.getInstance().getVisiblePlayer(pc)) {
							if (pc.getLocation().getTileLineDistance(member.getLocation()) <= 3 && member.getClanid() == pc.getClanid() && pc.getClanid() != 0 && member.getId() != pc.getId()) {
								L1Teleport.teleport(member, newX, newY, mapId, 5, true);
							}
						}
					}
					L1Teleport.teleport(pc, newX, newY, mapId, 5, true);
					pc.getInventory().removeItem(l1iteminstance, 1);
				} else {
					L1Teleport.teleport(pc, pc.getX(), pc.getY(), pc.getMapId(), pc.getHeading(), false);
					pc.sendPackets(new S_ServerMessage(79));
				}
			} else {
				if (pc.getMap().isTeleportable() || pc.isGm()) {
					L1Location newLocation = pc.getLocation().randomLocation(200, true);
					int newX = newLocation.getX();
					int newY = newLocation.getY();
					short mapId = (short) newLocation.getMapId();

					if (itemId == 40086) { // マステレポートスクロール
						for (L1PcInstance member : L1World.getInstance().getVisiblePlayer(pc)) {
							if (pc.getLocation().getTileLineDistance(member.getLocation()) <= 3 && member.getClanid() == pc.getClanid() && pc.getClanid() != 0 && member.getId() != pc.getId()) {
								L1Teleport.teleport(member, newX, newY, mapId, 5, true);
							}
						}
					}
					L1Teleport.teleport(pc, newX, newY, mapId, 5, true);
					pc.getInventory().removeItem(l1iteminstance, 1);
				} else {
					L1Teleport.teleport(pc, pc.getX(), pc.getY(), pc.getMapId(), pc.getHeading(), false);
					pc.sendPackets(new S_ServerMessage(276));
				}
			}
			cancelAbsoluteBarrier(pc); 
		//etc_scroll
		}
	}
	private void cancelAbsoluteBarrier(L1PcInstance pc) { // アブソルート バリアの解除
		if (pc.hasSkillEffect(ABSOLUTE_BARRIER)) {
			pc.killSkillEffectTimer(ABSOLUTE_BARRIER);
			pc.startHpRegeneration();
			pc.startMpRegeneration();
			pc.startMpRegenerationByDoll();
			pc.startHpRegenerationByDoll();//add 魔法娃娃回血功能
		}
	}
}
